load('js/v6/beijing/beijing_credit_collect.js');
load('js/v6/beijing/beijing_credit_crawl.js');
load('js/v6/beijing/beijing_common.js');
load('js/v6/beijing/beijing_lua.js');
load("js/luaclient.js");
load("js/keys_comm.js");

var billDate = "";// 账单日
/**
 * 北京信用卡登录
 */
var v6_beijing_credit_java_funcs = {
    openHomePage : function(){
        //访问首页
        var indexHtml = get(beijing_urls.REQ_LOGIN_HTML_URL);
        storeDataToFile(context, indexHtml, "index.html");
        //页面不为空
        context.put("indexHtml", indexHtml)
    },
    checkProxyAvailable: function () {

        var indexHtml = context.get("indexHtml")
        if(StringUtils.isBlank(indexHtml)) {
            return false
        }
        var doc = Jsoup.parse(indexHtml)
        logger.info("任务【 {} 】标题='{}'", context.get(LUA_TASK_KEY), doc.select("title").text())
        logger.info("任务【 {} 】id='username'的元素个数{}", context.get(LUA_TASK_KEY), doc.select("#username").size())

        return equals(doc.select("title").text(), "北京银行在线账户系统 北京银行在线账户系统") && doc.select("#username").size() > 0
    },
    checkTargetWebsiteAvailable: function(){ return true },
    checkControlAvailable:function (){ return true},
    //只有在图片验证码错误或短信验证码错误　　重置环境
    needResetEnvironment :function () {
        return  equals(context.get("currentErrorCode"),BALO_99602_50) || equals(context.get("currentErrorCode"),BALO_99602_40);
    },
    login: function() {
        // show()
        // 输入用户名密码
        var input_state = luaFuncs.input_account(getLoginParams(context)[0], context.get(LUA_TASK_TOKEN))
        //发送短信
        var alertMsg = luaFuncs.clickSms(); // 获取alert中的内容
        logger.info("点击短信时出现的:{}", alertMsg);
        var isSmsSendCorrectFuncs = function(alertMsg) {
            return alertMsg.contains("短信成功发送")
        }

        var getSmsSendErrMsgFuncs = function(alertMsg) {
            return alertMsg
        }
        assertTrueOrThrowException(alertMsg, isSmsSendCorrectFuncs, getSmsSendErrMsgFuncs)
        logger.info("任务【 {} 】短信发送成功", context.get(LUA_TASK_KEY))
        //接收短信
        var smsCode = waitSmsCodeBySeconds(context,120);
        // 给短信框输入内容
        set_input_text_by_id("smscode", smsCode);

        var smsCodeInputResult = luaFuncs.checkIfInput("smscode",smsCode);

        var isSmsCodeInputCorrectFunc = function(smsCodeInputResult) {
            return smsCodeInputResult
        }

        var getSmsCodeInputErrorMsgFunc = function(smsCodeInputResult) {
            return "短信验证码错误"
        }
        assertTrueOrThrowException(smsCodeInputResult, isSmsCodeInputCorrectFunc, getSmsCodeInputErrorMsgFunc)
        logger.info("任务【 {} 】短信验证码输入成功", context.get(LUA_TASK_KEY))


        //获取图片验证码
        var captchaPath = get_image_by_id("pwPic");

        logger.info("任务【 {} 】图片验证码路径【 {} 】", context.get(LUA_TASK_KEY), captchaPath)
        var isCaptchaPathCorrectFunc = function(captchaPath) {
            return captchaPath !== null && captchaPath !== undefined && captchaPath !== 'path fail'
        }

        var getCaptchaPathErrorMsgFunc = function(captchaPath) {
            return "图片验证码获取失败"
        }
        assertTrueOrThrowException(captchaPath, isCaptchaPathCorrectFunc, getCaptchaPathErrorMsgFunc)
        logger.info("任务【 {} 】图片验证码获取成功", context.get(LUA_TASK_KEY))

        //识别及判定
        context.put(TASK_IMG_PATH, captchaPath);
        context.put(TASK_IMG_REGEX,"[0-9]{6}");
        var imgCode = waitImgCodeBySecondsByJs(context,60,true,3);

        var isImgCodeCorrectFunc = function(imgCode) {
            return StringUtils.isNotBlank(imgCode) && imgCode.length === 6
        }

        var getImgCodeErrorMsgFunc = function(imgCode) {
            return "图片验证码错误"
        }
        assertTrueOrThrowException(imgCode, isImgCodeCorrectFunc, getImgCodeErrorMsgFunc)
        logger.info("任务【 {} 】图片验证码格式正确", context.get(LUA_TASK_KEY))

        show();
        //输入验证码
        set_input_text_by_id_event("code", imgCode);

        hide();

        var imgCodeInputResult = luaFuncs.checkIfInput("code",imgCode);

        var isImgCodeInputCorrectFunc = function(imgCodeInputResult) {
            return imgCodeInputResult
        }

        var getImgCodeInputErrorMsgFunc = function(imgCodeInputResult) {
            return "图片验证码未输入"
        }
        assertTrueOrThrowException(imgCodeInputResult, isImgCodeInputCorrectFunc, getImgCodeInputErrorMsgFunc)
        logger.info("任务【 {} 】图片验证码输入成功", context.get(LUA_TASK_KEY))


        //登录并判断是否成功
        var jsonObject = JSON.parseObject(luaFuncs.clickLoginBtn());

        var isLoginCorrectFuncs = function(jsonObject) {
            return Boolean.valueOf(jsonObject.get("login_result"))
        }

        var getLoginErrMsgFuncs = function(jsonObject) {
            return jsonObject.get("error_msg")
        }
        assertTrueOrThrowException(jsonObject, isLoginCorrectFuncs, getLoginErrMsgFuncs)
        logger.info("任务【 {} 】登录成功", context.get(LUA_TASK_KEY))

    },
    crawl:function () {
        logger.info("北京信用卡，任务{},抓取程序开始", context.get(LUA_TASK_KEY));
        var doneType = "DONE";
        Thread.sleep(3000);
        var creditCardAllDataList = new ArrayList();
        var creditCardAllData = new CreditCardAllData();
        var  billAllDatas = new ArrayList();
        creditCardAllData.setBills(billAllDatas);
        // 采集信用卡基本信息
        collectHtmlData.collectBillInfoData();
        crawCraditData.crawlBillInfoData(creditCardAllData);
        billDate = context.get("billDay");//账单日
        var monthList = commonUtils.getRecentMonth(7,"YYYY-MM",billDate);
        var nowMonth = monthList.get(0);
        logger.info("北京信用卡，任务 {} ，获取到月份集合，{}", context.get(LUA_TASK_KEY),monthList.toString())
        Collections.reverse(monthList);// 将集合的顺序颠倒,为了得到最后集合中最大的月份中的本期账单金额
        for(var monthListIndex = 0;monthListIndex < monthList.size();monthListIndex++){
            var singleMonth = monthList.get(monthListIndex);// 获取每个月
            var billAllData = new BillAllData();
            // 采集月账单列表信息
            collectHtmlData.collectBillDetails(singleMonth,billDate,doneType,nowMonth);
            // 抓取月账单列表信息
            crawCraditData.crawlBillDetails(creditCardAllData,billAllData,singleMonth,billDate,doneType);
            // 采集月账单详细信息
            collectHtmlData.collectBillDetail(singleMonth,doneType);
            // 抓取月账单详细信息
            crawCraditData.crawlBillDetail(creditCardAllData,billAllData,singleMonth,doneType);
        }
        doneType = "UNDONE";
        var undoneBillData = new BillAllData();
        // 采集未出账单列表信息
        collectHtmlData.collectBillDetails("",billDate,doneType,nowMonth);
        // 抓取月账单列表信息
        crawCraditData.crawlBillDetails(creditCardAllData,undoneBillData,"",billDate,doneType);
        // 采集月账单详细信息
        collectHtmlData.collectBillDetail("",doneType);
        // 抓取月账单详细信息
        crawCraditData.crawlBillDetail(creditCardAllData,undoneBillData,"",doneType);
        creditCardAllDataList.add(creditCardAllData);
        context.put(Constants.ALL_DATA, creditCardAllDataList);
    },
    exitLogin: function () {}
}